import React, { Component, ComponentClass } from 'react';
import Taro from '@tarojs/taro';
import {View} from '@tarojs/components'
import {GetRequest, vcmonitor} from "../utils/tools";
import {newHttp} from "../utils/request";
import {SELLER_PC_PAY, PC_CAR_PAY, PC_MOVIE_PAY, PC_LIANLIAN_PAY} from "../constants/api";
import {payment} from "../utils/payment.service";
import {PAY_URL} from "./enum";
import './index.scss'

type PageStateProps = {}

type PageDispatchProps = {
  getSet: any
}

type PageOwnProps = {}

type PageState = {}

type IProps = PageStateProps & PageDispatchProps & PageOwnProps

interface Login {
  props: IProps
}


class PayRoot extends Component {
  state: {
    infoData: any
    uuid: string
    payToken: string
    touuid: string
    productName: string
    sName: string
    type: string
    amount: number
    orderNum: number
  } = {
    uuid: '',
    payToken: '',
    infoData: {
      amount: 0,
      sName: '微程之家',
      productName: '商品信息'
    },
    productName: '',
    sName: '',
    touuid: '',
    amount: 0,
    type: '',
    orderNum: ''
  }

  async componentDidMount() {
    // const {type='',payToken='',uuid='',touuid='',amount,productName,sName ,orderNum} =  Taro.getCurrentInstance().router.params
    // this.setState({orderNum})

  }

  async componentDidShow() {
    vcmonitor(this)
    const {order_num} = Taro.getCurrentInstance().router.params
    this.setState({orderNum: order_num}, () => {
      this.goPay()
    })
  }

  async goPay() {
    const {orderNum} = this.state
    let order_num = Taro.getCurrentInstance().router.params.order_num;
    order_num = order_num.split('_');
    const type = order_num[1];
    let api = SELLER_PC_PAY;
    const userId = order_num[2];
    order_num = order_num[0];
    if (type == 1) {
      api = PC_CAR_PAY;
    }else if(type == 2){
      api = PC_MOVIE_PAY;
    }else if(type == 3){
      api = PC_LIANLIAN_PAY;
    }
    const result = await newHttp.catchPost(api + order_num, {
      userId
    });
    if(result.message) {
      Taro.showLoading({title: result.message, mask: true,})
      return;
    }
    if(result){
      payment.payment(result, false, res => {
        if (process.env.TARO_ENV === 'h5') {
          document.addEventListener('WeixinJSBridgeReady', function () {
            WeixinJSBridge.call('closeWindow');
          }, false);
          WeixinJSBridge.call('closeWindow');
        }
      })
    }
  }

  async goRecharge() {
    const {touuid, amount, type, uuid} = this.state
    const result = await newHttp.catchPost(PAY_URL[type].url + uuid, {payType: 'wxpay', amount, touuid})
    if (result) {
      payment.payment(JSON.parse(result.payCode), false, res => {
        if (process.env.TARO_ENV === 'h5') {
          document.addEventListener('WeixinJSBridgeReady', function () {
            WeixinJSBridge.call('closeWindow');
          }, false);
          WeixinJSBridge.call('closeWindow');
        }
      })
    }
  }

  render() {
    const {} = this.state
    return (
      <View className='pay-root'>
      </View>
    )
  }
}

export default PayRoot as ComponentClass<PageOwnProps, PageState>
